package com.offcn.service.impl;

import com.offcn.entity.*;
import com.offcn.mapper.ProductCategoryMapper;
import com.offcn.service.IProductCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service("productCategoryService")
public class ProductCategoryServiceImpl implements IProductCategoryService {
    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<ProductCategory> findFirstProductCategory() {
        ProductCategoryExample example=new ProductCategoryExample();
        ProductCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(0);
        return productCategoryMapper.selectByExample(example);
    }

    @Override
    public List<ProductCategoryVo> findProductCategory() {
        //声明一个装二三级目录的集合先
        List<ProductCategoryVo> productCategoryVoList=new ArrayList<>();
        //得到一级目录集合
        List<ProductCategory> firstProductCategoryList = findFirstProductCategory();

        //遍历一级
        for (ProductCategory firstProductCategory : firstProductCategoryList) {
            ProductCategoryExample example=new ProductCategoryExample();
            example.createCriteria().andParentidEqualTo(firstProductCategory.getId());
            List<ProductCategory> secondProductCategoryList = productCategoryMapper.selectByExample(example);
            //遍历二级目录
            for (ProductCategory secondProductCategory : secondProductCategoryList) {
                //先声明一个可以装二级目录，二级目录三级目录集合的对象
                ProductCategoryVo productCategoryVo=new ProductCategoryVo();
                //根据二级目录的id查找二级以下的三级目录
                ProductCategoryExample example2=new ProductCategoryExample();
                example2.createCriteria().andParentidEqualTo(secondProductCategory.getId());
                List<ProductCategory> thirdProductCategoryList = productCategoryMapper.selectByExample(example2);
                //把每个二级目录set进vo对象先
                productCategoryVo.setProductCategory(secondProductCategory);
                //将三级目录set进vo对象
                productCategoryVo.setProductCategoryList(thirdProductCategoryList);
                productCategoryVoList.add(productCategoryVo);
            }
        }
        return productCategoryVoList;
    }



 

}
